import scrapy
from douban.items import DoubanItem


class DoubanSpidersSpider(scrapy.Spider):
    #爬虫名
    name = 'douban_spiders'
    #允许的域名
    allowed_domains = ['movie.douban.com']
    #入口url，加入调度器中
    start_urls = ['https://movie.douban.com/top250']

    #默认的解析方法
    def parse(self, response):
        movie_list = response.xpath("//ol[@class='grid_view']/li")
        for i_item in movie_list:
            douban_item = DoubanItem()
            douban_item['serial_number'] = i_item.xpath('.//div[@class="item"]//em/text()').extract_first()
            douban_item['movie_name'] = i_item.xpath('.//span[@class="title"]/text()').extract_first()
            douban_item['introduce'] = i_item.xpath('.//div[@class="bd"]//p/text()').extract_first().strip()
            douban_item['star'] = i_item.xpath('.//span[@class="rating_num"]/text()').extract_first()
            douban_item['evaluate'] = i_item.xpath('.//div[@class="star"]//span[4]/text()').extract_first()
            douban_item['description'] = i_item.xpath('.//span[@class="inq"]/text()').extract_first()
            #yield情绪数据
            yield douban_item
        # 解析下一页
        next_link = response.xpath('//span[@class="next"]/link/@href').extract()
        if next_link:
            next_link = next_link[0]
            yield scrapy.Request("https://movie.douban.com/top250"+next_link, callback=self.parse)
